package com.pd.servlet;

import com.pd.pojo.WxAccount;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by dong peng
 * DATE : 2017/2/20
 * TIME : 21:09.
 */

@Controller
public class UserServlet extends HttpServlet {

    @RequestMapping("/UserServlet.do")
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
System.out.println("-----------------------");
System.out.println(req.getParameterMap());
        String resource = "db/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession session = sqlSessionFactory.openSession();
        try {
            WxAccount accessToken = session.selectOne("com.pd.pojo.WxAccount.selectAccessToken",1);
            System.out.println(accessToken);
            System.out.println(accessToken.getId());
            System.out.println(accessToken.getAppId());
            System.out.println(accessToken.getAppSecret());
        } finally {
            session.close();
        }
    }

    @RequestMapping("/aa.do")
    public void aaa() throws IOException {
        System.out.println("dddddd");
        String resource = "db/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession session = sqlSessionFactory.openSession();
        try {
            WxAccount accessToken = session.selectOne("com.pd.pojo.WxAccount.selectAccessToken",1);
            System.out.println(accessToken);
            System.out.println(accessToken.getId());
            System.out.println(accessToken.getAppId());
            System.out.println(accessToken.getAppSecret());
        } finally {
            session.close();
        }

//        return "SUCCESS";
    }


    private int count = 0;
    Service s = new Service();
    @RequestMapping("/testThressad.do")
    public void testThread(HttpServletRequest req,  HttpServletResponse resp) throws IOException, InterruptedException {
        System.out.println("request entry ......" + Thread.currentThread().getName());
        String name = req.getParameter("name");
        System.out.println("name = " +name);
        count++;
        System.out.println("count = "+ count);

        s.add();
        System.out.println("num   "+ s.getAdd());
        System.out.println("request end ......" + Thread.currentThread().getName());
        System.out.println("<---------------------------------------------------------->");

        resp.getOutputStream().write(10);
    }
}
